﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VeryExtremeArt.Controller
{
    public class ArtPieceController
    {

        public ArtPiece GetArtPiece(int Id)
        {
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                return db.ArtPieces.SingleOrDefault(ap => ap.Id == Id);
            }
        }
        public IList<ArtPiece> GetArtPieces(ArtPiece artPiece)
        {
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                List<ArtPiece> artPieceList = new List<ArtPiece>();
                if (!string.IsNullOrEmpty(artPiece.Name))
                {
                    if (!string.IsNullOrEmpty(artPiece.Artist))
                    {
                        return db.ArtPieces.Where(ap => ap.Name.ToUpper().Contains(artPiece.Name.ToUpper()) && ap.Description.ToUpper().Contains(artPiece.Description.ToUpper())).ToList();
                    }
                }
                if (!string.IsNullOrEmpty(artPiece.Artist))
                {
                    return db.ArtPieces.Where(ap => ap.Name.ToUpper().Contains(artPiece.Name.ToUpper())).ToList();
                }
                if (!string.IsNullOrEmpty(artPiece.Description))
                {
                    return db.ArtPieces.Where(ap => ap.Description.ToUpper().Contains(artPiece.Description.ToUpper())).ToList();
                }
                return artPieceList;
            }
        }
        public ArtPiece UpdateArtPiece(int Id, ArtPiece artPiece)
        {
            ArtPiece artPieceFromDb = GetArtPiece(Id);
            if (artPieceFromDb == null)
                return null;
            if (artPiece.Description.Equals("") || artPiece.Artist.Equals("") || artPiece.Name.Equals("")) return null;

            artPieceFromDb.Artist = artPiece.Artist;
            artPieceFromDb.Description = artPiece.Description;
            artPieceFromDb.Name = artPiece.Name;
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                db.SubmitChanges();
            }
            return artPieceFromDb;
        }
        public ArtPiece InsertArtPiece(ArtPiece artPiece)
        {
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                if (string.IsNullOrEmpty(artPiece.Artist) || string.IsNullOrEmpty(artPiece.Name) || string.IsNullOrEmpty(artPiece.Description))
                {
                    return null;
                }
                else
                {
                    db.ArtPieces.InsertOnSubmit(artPiece);
                    db.SubmitChanges();
                    //ArtPiece ap = db.ArtPieces.SingleOrDefault(app => app.Name == artPiece.Name && app.Description.ToString() == artPiece.Description.ToString() && app.Artist == artPiece.Artist);
                    return artPiece;
                }
            }
        }
        public bool DeleteArtPiece(ArtPiece artPiece)
        {
            ArtPiece dbArtPiece = GetArtPiece(artPiece.Id);
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                try
                {
                    db.ArtPieces.DeleteOnSubmit(dbArtPiece);
                    db.SubmitChanges();
                    return true;
                }
                catch (Exception e)
                {

                    throw e;
                }
            }
        }
        public decimal GetCurrentItemPriceForNextAuction(int artPieceId)
        {
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                AuctionController ac = new AuctionController();
                decimal theResult = db.AuctionedItems.FirstOrDefault(ai => ai.ArtPiece.Id == artPieceId && ai.AuctionId == ac.GetNextAuction().Id).CurrentPrice == null ? 0 : (decimal)db.AuctionedItems.FirstOrDefault(ai => ai.ArtPiece.Id == artPieceId && ai.AuctionId == ac.GetNextAuction().Id).CurrentPrice;
                return theResult;
            }
        }
        public AuctionedItem GetAuctionItem(int artPieceId)
        {
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                AuctionController ac = new AuctionController();
                return db.AuctionedItems.SingleOrDefault(ai => ai.ArtPieceId == artPieceId && ai.AuctionId == ac.GetNextAuction().Id);
            }
        }
    }

}